package org.leaf.green.b2b

class DistributionController {

    def distributionService

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        render view : '/b2b/distribution/list', model : [distributionInstanceList: Distribution.list(params), distributionInstanceTotal: Distribution.count()]
    }

    def create = {
        def distributionInstance = distributionService.init()
        //distributionInstance.properties = params
        render view : '/b2b/distribution/create', model : [distributionInstance: distributionInstance]
    }

    def save = {
        def distributionInstance = new Distribution(params)
        if (!distributionInstance.hasErrors() && distributionInstance.save()) {
            flash.message = "distribution.created"
            flash.args = [distributionInstance.id]
            flash.defaultMessage = "Distribution ${distributionInstance.id} created"
            redirect(action: "show", id: distributionInstance.id)
        }
        else {
            render(view: "/b2b/distribution/create", model: [distributionInstance: distributionInstance])
        }
    }

    def show = {
        def distributionInstance = Distribution.get(params.id)
        if (!distributionInstance) {
            flash.message = "distribution.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Distribution not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/b2b/distribution/show', model : [distributionInstance: distributionInstance]
        }
    }

    def edit = {
        def distributionInstance = Distribution.get(params.id)
        if (!distributionInstance) {
            flash.message = "distribution.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Distribution not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/b2b/distribution/edit', model : [distributionInstance: distributionInstance]
        }
    }

    def update = {
        def distributionInstance = Distribution.get(params.id)
        if (distributionInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (distributionInstance.version > version) {
                    
                    distributionInstance.errors.rejectValue("version", "distribution.optimistic.locking.failure", "Another user has updated this Distribution while you were editing")
                    render(view: "/b2b/distribution/edit", model: [distributionInstance: distributionInstance])
                    return
                }
            }
            distributionInstance.properties = params
            if (!distributionInstance.hasErrors() && distributionInstance.save()) {
                flash.message = "distribution.updated"
                flash.args = [params.id]
                flash.defaultMessage = "Distribution ${params.id} updated"
                redirect(action: "show", id: distributionInstance.id)
            }
            else {
                render(view: "/b2b/distribution/edit", model: [distributionInstance: distributionInstance])
            }
        }
        else {
            flash.message = "distribution.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Distribution not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def distributionInstance = Distribution.get(params.id)
        if (distributionInstance) {
            try {
                distributionInstance.delete()
                flash.message = "distribution.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Distribution ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "distribution.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Distribution ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "distribution.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Distribution not found with id ${params.id}"
            redirect(action: "list")
        }
    }
}
